#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <cstring>
#include <arpa/inet.h>
#include <unistd.h>
#include <algorithm>
#include <cctype>
#include <json-c/json.h>
#include "StdShared.h"
using namespace std;

#define SERVER_PORT 8080
#define BUFFER_SIZE 128
#define SIZE 100

struct userInfo
{
    int type=0;
    char name[SIZE];
    char passwd[SIZE];
};

// void recvClient(int connfd)
// {
//     char buffer[BUFFER_SIZE]={0};
//     memset(buffer, 0, sizeof(buffer));

//     // 1. 读取数据到缓冲区
//     int readBytes = read(connfd, buffer, sizeof(buffer));
//     if (readBytes > 0) 
//     {
//         cout << "接收到用户信息..." << buffer << endl;
//         // 2. 解析 JSON
//         json_object *jsonObj = json_tokener_parse(buffer);
//         if (jsonObj != nullptr) 
//         {
//             // 3. 获取字段
//             json_object *jtype, *jname, *jpasswd;
//             json_object_object_get_ex(jsonObj, "type", &jtype);
//             json_object_object_get_ex(jsonObj, "name", &jname);
//             json_object_object_get_ex(jsonObj, "passwd", &jpasswd);

//             // 4. 打印结果
//             cout << "type: " << json_object_get_int(jtype) << endl;
//             cout << "姓名: " << json_object_get_string(jname) << endl;
//             cout << "密码: " << json_object_get_string(jpasswd) << endl;

//             // 5. 释放 JSON 对象
//             json_object_put(jsonObj);
//         }
//         else
//         {
//             cout<<"解析json失败"<<endl;
//         }
//     }
//     else
//     {
//         cout<<"读取失败"<<endl;
//     }

    // userInfo info;
    // memset(&info,0,sizeof(info));

    // int readBytes = read(connfd, &info, sizeof(info));
    // if (readBytes > 0) 
    // {
    //     cout << "接收到用户信息..." << endl;
    //     cout << "type: " << info.type<<endl;
    //     cout << "姓名: " << info.name << endl;
    //     cout << "密码: " << info.passwd << endl;
    // } 
    // else 
    // {
    //     cerr << "读取失败" << endl;
    // }
// }

int main()
{
    /*创建socket套接字*/
    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    if(sockfd==-1)
    {
        perror("socket error");
        exit(-1);
    }
    cout<<"sockfd: "<<sockfd<<endl;

    /*设置端口复用*/
    int optVal=1;
    setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&optVal,sizeof(optVal));

    /*绑定IP和端口信息*/
    struct sockaddr_in localAddress;
    /*清空结构体的脏数据*/
    memset(&localAddress,0,sizeof(localAddress));
    /*地址族是IPV4*/
    localAddress.sin_family=AF_INET;//localAddress.sa_family=AF_INET;
    localAddress.sin_port=htons(SERVER_PORT);
    /*设置全通规则*/
    localAddress.sin_addr.s_addr=htonl(INADDR_ANY);
    bind(sockfd,reinterpret_cast<struct sockaddr*>(&localAddress),sizeof(localAddress));
    /*设置监听*/
    listen(sockfd,10);
    cout<<"服务器阻塞等待客户端接入..."<<endl;

    /*通信句柄*/
    int connfd=accept(sockfd,nullptr,nullptr);//阻塞函数
    cout<<"有人跟我连接了..."<<connfd<<endl;
    
    /*读数据*/
    char readBuffer[BUFFER_SIZE];
    /*清空缓冲区*/
    // memset(readBuffer,0,sizeof(readBuffer));
    // int readBytes=0;
    while(1)
    {
        //recvClient(connfd);

        // readBytes=read(connfd,readBuffer,sizeof(readBuffer));
        // if(readBytes>0)
        //     cout<<"readBytes: "<<readBytes<<" ,readBuffer: "<<readBuffer<<endl;
        // if(strcmp(readBuffer,"exit")==0)
        // {
        //     cout<<"正在退出服务器..."<<endl;
        //     sleep(1);
        //     exit(-1);
        // }

        // /*将小写转换为大写*/
        // for(int i=0;i<readBytes;i++)
        // {
        //     readBuffer[i]=toupper(readBuffer[i]);
        // }
        // /*写回客户端*/
        // write(connfd,readBuffer,strlen(readBuffer));

        memset(readBuffer, 0, sizeof(readBuffer));
        int readBytes = read(connfd, readBuffer, sizeof(readBuffer));
        cout<<"readBytes: "<<readBytes<<" ,readBuffer: "<<readBuffer<<endl;

        /*1.将json字符串解析成json对象*/
        json_object *jsonObj=json_tokener_parse(readBuffer);

        /*根据key得到value*/
        // json_object *typeVal=json_object_object_get(jsonObj,"type");
        // cout<<"type: "<<json_object_get_int(typeVal)<<endl;

        int type=json_object_get_int(json_object_object_get(jsonObj,"type"));
        const char *name=json_object_get_string(json_object_object_get(jsonObj,"name"));
        const char *passwd=json_object_get_string(json_object_object_get(jsonObj,"passwd"));
        if(type==REGISTER)
        {
            cout<<"注册"<<endl;
            cout<<"姓名: "<<name<<endl;
            cout<<"密码: "<<passwd<<endl;
        }
        else if(type==LOGIN)
        {
            cout<<"登录"<<endl;
            cout<<"姓名: "<<name<<endl;
            cout<<"密码: "<<passwd<<endl;
        }
        
    }
    return 0;
}
